package com.hue.springbootemail.email;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * 发送邮件的工具类
 */
@Component
public class EmailUtils {
    @Autowired
    private JavaMailSender mailSender;

    @Value("${spring.mail.username}")
    private String from;

    @Async  //异步发送邮件
    public void send(String to, String subject, String text) {
        SimpleMailMessage message = new SimpleMailMessage();
        try {
            message.setFrom(from);
            //接收者
            message.setTo(to);
            //标题
            message.setSubject(subject);
            //内容
            message.setText(text);
            mailSender.send(message);
        } catch (Exception e) {
        }
    }
}
